#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion



using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Text;

namespace IPSim.GUI.Devices
{
    public class RectangleDeviceController: IMoveController
    {
        #region Members

        protected BaseDevice controlledDevice;
        private bool isMoving = false;
        private Point moveOffset = new Point();

        #endregion

        #region Methods

        public RectangleDeviceController(BaseDevice device)
        {
            controlledDevice = device;
        }


        #region IMoveController Members

        public bool IsMoving
        {
            get { return isMoving; }
        }

        public void Start(Point startPoint)
        {
            moveOffset.X = controlledDevice.Location.X - startPoint.X;
            moveOffset.Y = controlledDevice.Location.Y - startPoint.Y;
            isMoving = true;
        }

        public void Move(Point currentPoint)
        {
            if (isMoving)
            {
                Point newDeviceLocation = currentPoint;
                newDeviceLocation.Offset(moveOffset.X, moveOffset.Y);
                if (newDeviceLocation.X < 0) newDeviceLocation.X = 0;
                if (newDeviceLocation.Y < 0) newDeviceLocation.Y = 0;
                controlledDevice.Location = newDeviceLocation;
            }
        }

        public void End()
        {
            isMoving = false;
        }

        #endregion

        #region IController Members

       
        public bool IsInside(Point p)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddRectangle(controlledDevice.GetRectangle());
            return gp.IsVisible(p);
        }

        public void DrawSelection(Graphics g)
        {
            int borderWidth = 3;

            Rectangle r = new Rectangle(
                controlledDevice.Location.X - borderWidth, controlledDevice.Location.Y - borderWidth,
                controlledDevice.Size.Width + (borderWidth * 2), controlledDevice.Size.Height + (borderWidth * 2));

            HatchBrush brush = new HatchBrush(HatchStyle.SmallCheckerBoard, Color.LightGray, Color.Transparent);
            Pen p = new Pen(brush, borderWidth);
            g.DrawRectangle(p, r);
        }

        #endregion

        #endregion

        #region Properties

        public BaseDevice ControlledDevice
        {
            get { return controlledDevice; }
        }

        #endregion
    }
}
